//
//  NSString+Enhanced.m
//  SMCKit
//
//  Created by Ren JianSheng on 11-5-4.
//  Copyright 2011年 3gtv.net. All rights reserved.
//

#import "NSString+QL.h"

@implementation NSString (QL)

+ (BOOL)ql_isEmptyString:(NSString *)string {
    if(!string) {
        return YES;
    }
    NSString *result = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    return [@"" isEqualToString:result];
}

- (NSString *) ql_stringsAppend:(NSString *)params, ...{
    NSMutableString *result = [NSMutableString string];
    [result appendString:self];
    va_list args;
    va_start(args, params);
    for (NSString *arg = params; arg != nil; arg = va_arg(args, NSString*)) {
        [result appendString:arg];
    }
    va_end(args);
    return result;
}

- (NSString *) ql_stringsByAppendingPathComponent:(NSString *)params, ... {
    NSString *result = @"";
    result = [result stringByAppendingString:self];
    va_list args;
    va_start(args, params);
    for (NSString *arg = params; arg != nil; arg = va_arg(args, NSString*)) {
        result = [result stringByAppendingString:@"/"];
        result = [result stringByAppendingString:arg];
    }
    va_end(args);
    return result;
}

- (BOOL)ql_isNewerVersionThan:(NSString*)otherVersion {  
    return ([self compare:otherVersion options:NSNumericSearch] == NSOrderedDescending);  
}

- (NSString *)ql_stringWithLine {
    return [self stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
}

- (int)ql_count {
    NSArray *words = [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return (int)[words count];
}

- (CGSize)ql_computStringHeightWithFont:(UIFont*)font withMaxWidth:(CGFloat)maxwidth withMaxHeight:(CGFloat)maxHeight
{
    CGSize maxSize = CGSizeMake(maxwidth, maxHeight);
     return [self sizeWithFont:font constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
}

//查找URL中的文件名
+(NSString*) fileNameInURL:(NSString*)url
{
    if(url == nil)
        return nil;
    NSString *tmpStr;
    NSRange endFlatLoc = [url rangeOfString:@"?"];
    if(endFlatLoc.location != NSNotFound){
        tmpStr = [url substringToIndex:endFlatLoc.location];
    }else{
        tmpStr = url;
    }
    
    NSRange tmpLoc;
    int startIndex = 0;
    while (true) {
        if((tmpLoc = [tmpStr rangeOfString:@"/"]).location == NSNotFound){
            break;
        }
        startIndex = (int)tmpLoc.location + (int)tmpLoc.length;
        tmpStr = [tmpStr substringFromIndex:startIndex];
    }
    NSLog(@"fileName:%@",tmpStr);
    return tmpStr;
}

@end
